package com.ruoyi.api.service.impl;

import com.ruoyi.api.domain.GuidDayEntity;
import com.ruoyi.api.domain.GuidDayScenicEntity;
import com.ruoyi.api.domain.GuidEntity;
import com.ruoyi.api.service.GuidDayScenicService;
import com.ruoyi.api.service.GuidDayService;
import com.ruoyi.api.service.UserGuidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserGuidServiceImpl implements UserGuidService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private GuidDayService guidDayService;
    @Autowired
    private GuidDayScenicService guidDayScenicService;
    @Override
    public void savePlans(GuidEntity guidEntity) {
        guidDayService.deleteByGuidId(guidEntity.getId());
        guidDayScenicService.deleteByGuidId(guidEntity.getId());
        for (Map<String,Object> objectMap:
                guidEntity.getPlans()) {
            GuidDayEntity guidDayEntity = new GuidDayEntity();
            if(objectMap.containsKey("date")){
                guidDayEntity.setDate(objectMap.get("date").toString());
            }
            if(objectMap.containsKey("name")){
                guidDayEntity.setName(objectMap.get("name").toString());
            }
            if(objectMap.containsKey("title")){
                guidDayEntity.setName(objectMap.get("title").toString());
            }
            guidDayEntity.setGuidId(guidEntity.getId());
            GuidDayEntity guidDayEntity1 = guidDayService.save(guidDayEntity);


            if(objectMap.containsKey("content")){
                List<Map<String,Object>> list = (List<Map<String, Object>>) objectMap.get("content");
                List<GuidDayScenicEntity> guidDayScenicEntityList = new ArrayList<>();
                for (Map<String,Object> map:
                list) {
                    GuidDayScenicEntity guidDayScenicEntity = new GuidDayScenicEntity();
                    if(map.containsKey("id")){
                        guidDayScenicEntity.setScenicId(map.get("id").toString());
                    }
//                    if(map.containsKey("scenicId")){
                        guidDayScenicEntity.setGuidDayId(guidDayEntity1.getId());
//                    }
                    if(map.containsKey("startTime")&& null!=map.get("startTime")){
                        guidDayScenicEntity.setStartTime(map.get("startTime").toString());
                    }
                    if(map.containsKey("endTime")&& null!=map.get("endTime")){
                        guidDayScenicEntity.setEndTime(map.get("endTime").toString());
                    }
                    if(map.containsKey("sort")&& null!=map.get("sort")){
                        guidDayScenicEntity.setSort(Integer.valueOf(map.get("sort").toString()));
                    }
                    if(map.containsKey("remark") && null!=map.get("remark")){
                        guidDayScenicEntity.setRemark(map.get("remark").toString());
                    }
                    if(map.containsKey("time")&& null!=map.get("time")){
                        guidDayScenicEntity.setTime(map.get("time").toString());
                    }
//                    if(map.containsKey("sort")){
                        guidDayScenicEntity.setGuidId(guidEntity.getId());
//                    }
                    guidDayScenicEntityList.add(guidDayScenicEntity);
                }
                guidDayScenicService.save(guidDayScenicEntityList);
            }


        }
    }
}
